import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: OverlayDemo(),
    );
  }
}

class OverlayDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Overlay Demo')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showOverlay(context);
          },
          child: Text('Show Overlay'),
        ),
      ),
    );
  }

  void _showOverlay(BuildContext context) {
    // 创建一个 OverlayEntry
    final overlay = Overlay.of(context);
    final overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: 0,
        left: 0,
        child: Material(
          color: Colors.transparent,
          child: Container(
            width: 200,
            height: 100,
            color: Colors.blue,
            child: Center(
              child: Text(
                'This is an overlay!',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );

    // 将 OverlayEntry 插入到 Overlay 中
    overlay.insert(overlayEntry);

    // 在一定时间后移除 OverlayEntry
    Future.delayed(Duration(seconds: 2), () {
      overlayEntry.remove();
    });
  }
}
